﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slb.Ocean.Core;
using Slb.Ocean.Petrel.IO.Attributes;

namespace Public.Ocean.Petrel.Extensions.Attributes
{
    public static class CustomAttributes
    {
        private static void GetServices(object data,
            out IDataSourceManager manager,
            out ICustomAttributeService attributeService,
            out CustomAttributeEventService eventService)
        {
            manager = DataManager.DataSourceManager;
            attributeService = CoreSystem.GetService<ICustomAttributeService>();
            eventService = CustomAttributeEventService.Instance;
        }

        public static TAttributes AttributeFor<TAttributes>(IIdentifiable owner) where TAttributes : CustomAttributesBase, new()
        {
            Droid droid = owner != null ? owner.Droid : Droid.Empty;

            IDataSourceManager manager;
            ICustomAttributeService attributeService;
            CustomAttributeEventService eventService;
            GetServices(owner, out manager, out attributeService, out eventService);

            var result = new TAttributes();
            CustomAttributesBase.Initialize(
                result, droid, 
                manager, attributeService, eventService);
            CustomAttributesBase.SetCachedData(result, owner);
            return result;
        }

        public static bool HasAttributeFor<TAttributes>(IIdentifiable owner) where TAttributes : CustomAttributesBase, new()
        {
            Droid droid = owner != null ? owner.Droid : Droid.Empty;

            IDataSourceManager manager;
            ICustomAttributeService attributeService;
            CustomAttributeEventService eventService;
            GetServices(owner, out manager, out attributeService, out eventService);

            return CustomAttributesBase.HasAttributes<TAttributes>(droid, manager, attributeService);
        }
    }
}
